package com.youlu.campus.web.recycle;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.recycle.dto.RecycleItemSearchDTO;
import com.youlu.campus.entity.recycle.dto.RecycleMyOrderDTO;
import com.youlu.campus.entity.recycle.dto.RecycleOrderCreateDTO;
import com.youlu.campus.service.recycle.RecycleMiniService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 回收控制器
 * 处理回收相关的订单创建、门店查询、订单列表及商品搜索等操作
 *
 * @author : [dengbo]
 * @createTime : [2022/6/23 16:29]
 */
@Slf4j
@RestController
@RequestMapping("/mini-app/")
public class RecycleController {

    @Autowired
    private RecycleMiniService recycleMiniService;

    /**
     * 创建回收订单
     * @param request 回收订单创建DTO，包含订单相关信息
     * @param httpRequest HTTP请求对象，用于获取客户端IP
     * @return 订单创建结果
     */
    @PostMapping("/recycle/order/create")
    public QResult createOrder(@Validated @RequestBody RecycleOrderCreateDTO request, HttpServletRequest httpRequest) {
        String userId = RequestContext.getUserId();
        log.info("RecycleController,courseInfo,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        request.setClientIp(CommonUtils.getRemoteIp(httpRequest));
        return QResult.success(recycleMiniService.createOrder(request));
    }

    /**
     * 获取回收店铺列表
     * 查询所有可用的回收门店信息
     * @return 回收店铺列表数据
     */
    @PostMapping("/recycle/store/list")
    public QResult getStoreList() {
        return QResult.success(recycleMiniService.getStoreList());
    }

    /**
     * 获取我的回收订单列表
     * 查询当前用户的回收订单列表，支持分页等查询条件
     * @param request 我的订单查询DTO，包含分页及筛选条件
     * @return 回收订单列表数据
     */
    @PostMapping("/recycle/my/order/list")
    public QResult myOrderList(@Validated @RequestBody RecycleMyOrderDTO request) {
        String userId = RequestContext.getUserId();
        log.info("RecycleController,myOrderList,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        return QResult.success(recycleMiniService.myOrderList(request));
    }

    /**
     * 回收商品搜索
     * 根据搜索条件查询可回收的商品信息
     * @param request 商品搜索DTO，包含搜索关键词等条件
     * @return 匹配的商品列表数据
     */
    @PostMapping("/recycle/item/search")
    public QResult search(@Validated @RequestBody RecycleItemSearchDTO request) {
        String userId = RequestContext.getUserId();
        log.info("RecycleController,search,userId:{},request param:{}", userId, JSON.toJSONString(request));
        return QResult.success(recycleMiniService.search(request));
    }
}